package cn.wolfcode.shop.service.impl;

import cn.wolfcode.shop.domain.SeckillOrder;
import cn.wolfcode.shop.mapper.SeckillOrderMapper;
import cn.wolfcode.shop.redis.RedisTemplate;
import cn.wolfcode.shop.redis.SeckillServerKeyPrefix;
import cn.wolfcode.shop.service.ISeckillOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SeckillOrderServiceImpl implements ISeckillOrderService {

    @Autowired
    private SeckillOrderMapper seckillOrderMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    public SeckillOrder findByUserIdAndSeckillGoodId(Long userId, Long seckillGoodId) {
        return seckillOrderMapper.findByUserIdAndSeckillGoodId(userId,seckillGoodId);
    }

    public void createSeckillOrder(Long userId, Long seckillGoodId, String orderNo) {
        SeckillOrder seckillOrder = new SeckillOrder();
        seckillOrder.setOrderNo(orderNo);
        seckillOrder.setSeckillGoodId(seckillGoodId);
        seckillOrder.setUserId(userId);
        seckillOrderMapper.insert(seckillOrder);
        // 将秒杀订单放入到redis中
        redisTemplate.set(SeckillServerKeyPrefix.SECKILL_ORDER,userId + ":"+seckillGoodId,seckillOrder);
    }
}
